package com.boesch.electroOffice.presentation.customer;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.portlet.PortletSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.business.businessObject.CustomerBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.model.messerli.Customer;
import com.boesch.electroOffice.utils.MessageUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * This class controls all the data is needed 
 * in the above package com.boesch.electroOffice.presentation.customer
 * 
 * For more details about the package layering have a look at the documentation
 * of the project electroOffice or the project object model pom.xml
 * 
 * @author Ralph Koster
 *
 */
@Controller
@ManagedBean
@SessionScoped
public class CustomerListController implements Serializable {

	private static final long serialVersionUID = 2302432178194437288L;
	private static final Log log = LogFactoryUtil.getLog(CustomerListController.class);
	
	@Autowired
	private CustomerBusinessObject customerBusinessObject;
	@Autowired
	private CustomerNavigationController navigationController;
	
	private List<Customer> customers;
	private List<Customer> filteredCustomers;
	
	private Customer selectedCustomer;
	
	private boolean customersInitialized = false;

	public List<Customer> getCustomers() {
		if(null == customers || !customersInitialized) {
			try {
				customers = customerBusinessObject.readAllCustomers();
				customersInitialized = true;
			} catch (IllegalDataAccessException e) {
				log.error("Failed to load the list of Customers.", e.getCause());
			} catch (BusinessException e) {
				log.error("Failed to load the list of Customers.", e.getCause());
			}
		}
		return customers;
	}
	
	public void navigateToCustomerView(ActionEvent actionEvent) {
		setSessionAttributeSelectedCustomerId();
		String navigationPath = "/jsf/customer/customer_detail.xhtml";
		navigateToPath(navigationPath);
	}
	
	public void navigateToCustomerEdit(ActionEvent actionEvent) {
		setSessionAttributeSelectedCustomerId();
		String navigationPath = "/jsf/customer/customer_form.xhtml";
		navigateToPath(navigationPath);
	}
	
	public void refreshLists(ActionEvent actionEvent) {
		customersInitialized = false;
	}
	
	public List<Customer> getFilteredCustomers() {
		return filteredCustomers;
	}

	public void setFilteredCustomers(List<Customer> filteredCustomers) {
		this.filteredCustomers = filteredCustomers;
	}
	
	public Customer getSelectedCustomer() {  
        return selectedCustomer;  
    }
	
    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;  
    }
    
    public boolean isOneCustomerSelected() {
    	boolean returnValue = false;
    	if(null != selectedCustomer) {
    		returnValue = true;
    	}
    	return returnValue;
    }
    
    private void navigateToPath(String navigationPath) {
		if(null != selectedCustomer) {
			int customerId = selectedCustomer.getCustomerId();
			PortletSession session = PortletUtils.getPortletSession();
			session.setAttribute("customerId", customerId);
			navigationController.setNavigationPath(navigationPath);
		} else {
			MessageUtils.addErrorMessage("Bitte selektieren Sie einen Auftrag aus der Liste!");
		}
	}
	
	private void setSessionAttributeSelectedCustomerId() {
		if(null != selectedCustomer) {
			PortletSession session = PortletUtils.getPortletSession();
			session.setAttribute("customerId", selectedCustomer.getCustomerId());
		}
	}
}